package gui.consumption;

import dao.db;
import gui.alert;
import pojo.Consumption;
import pojo.Guest;
import pojo.Guestroominfo;
import service.consumptionService;
import service.guestService;
import service.roomService;
import util.JTableDataUtil;
import util.Utli_tools;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Date;
import java.util.List;

public class add_order {
    private JPanel add_room;
    private JButton confirm_add_Button;
    private JTextField room_type;
    private JTextField days;
    private JTable table1;
    private JFormattedTextField start_date;
    private JFormattedTextField end_date;
    private JButton 查询Button;
    private JTextField room_id;
    private JTextField user_id;
    private JTextArea user_info;
    public Guest guestTmp;

    public add_order() {
        Utli_tools.setJFormattedTextField_date(start_date);
        Utli_tools.setJFormattedTextField_date(end_date);

        confirm_add_Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                Consumption c = new Consumption();
                c.setStartTime(new Date(((java.util.Date) start_date.getValue()).getTime()));
                c.setEndTime(new Date(((java.util.Date) end_date.getValue()).getTime()));
                c.setDays(db.getBetweenDayNumber(start_date.getText(), end_date.getText()));

                Guestroominfo guestroominfo1 = null;

                List<Guestroominfo> list1 = roomService.queryByid(Integer.parseInt(room_id.getText()));
                if (list1.size() != 0) {
                    guestroominfo1 = list1.get(0);
                }
                c.setGuestId(guestTmp.getId());
                c.setGusetName(guestTmp.getBname());
                c.setRoomId(guestroominfo1.getId());
                c.setRoomType(guestroominfo1.getGrank());
                c.setComment("未入住");
                c.setFee(guestroominfo1.getGrent() * c.getDays());


                guestTmp.setIsInRoom("未入住");
                guestService.updateByid(guestTmp);

                if (consumptionService.insert(c) == 1) {
                    alert.show("添加成功");
                } else {
                    alert.show("添加失败");
                }
            }
        });
        查询Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                List<Guest> list = guestService.queryByid(Integer.parseInt(user_id.getText()));
                Guest guest = null;
                if (list.size() != 0) {
                    guest = list.get(0);
                }
                guestTmp = guest;
                user_info.setText(guest.toString());
                queryAllRooms();
            }
        });
    }

    public void queryAllRooms() {
        final String[] columnNames = {"序号", "类型", "租金", "偏好"};
        List<Guestroominfo> storeList = roomService.queryByTimeNotConfict(
                new Date(((java.util.Date) start_date.getValue()).getTime()),
                new Date(((java.util.Date) end_date.getValue()).getTime()));
        JTableDataUtil<Guestroominfo> jTableDataUtil = new JTableDataUtil<>(Guestroominfo.class);

        TableModel dataModel = new DefaultTableModel(jTableDataUtil.getObjData(storeList), columnNames);
        table1.setModel(dataModel);
    }

    public static void show() {
        JFrame frame = new JFrame("预定客房");
        frame.setContentPane(new add_order().add_room);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

}
